﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Common;
using System.IO;
using System.Configuration;
using System.Xml;
namespace DevEx
{
    public partial class frmChooseDB : Form
    {

        public frmChooseDB()
        {
            InitializeComponent();
        }

        private void Backup_Load(object sender, EventArgs e)
        {
            try
            {
                cmbServer.Text = Thanh.Utilities.XL_DULIEU.LayGiaTriThanhPhan("Data Source");
                LoadDatabase();
            }
            catch (Exception)
            {MessageBox.Show("Không load được cơ sở dữ liệu!", "Lỗi");
            }
            
            
        }   

        private void btn_backup_Click(object sender, EventArgs e)
        {
            //MessageBox.Show(global::DevEx.Properties.Settings.Default.QLVTConnectionString, "Thành công");
            string connectString = "";
            try
            {
                var xmlDoc = new XmlDocument();
                xmlDoc.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);

                string server = cmbServer.Text;
                string db = cmbDataBase.Text ;
                string user = txt_username.Text ;
                string pass = txt_password.Text ;
                connectString = String.Format("Data Source={0};Initial Catalog={1};Persist Security Info=True;User ID={2};Password={3}", server, db, user, pass);

                XmlNode node =
                    xmlDoc.SelectSingleNode(
                        "//connectionStrings/add[@name='DevEx.Properties.Settings.QLVTConnectionString']");
                node.Attributes["connectionString"].Value = connectString;
                xmlDoc.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);

                ConfigurationManager.RefreshSection("connectionStrings");

               
            }catch (Exception ex)
            {
                throw ex;
            }
            //hiển thị lại config
            //MessageBox.Show(global::DevEx.Properties.Settings.Default.QLVTConnectionString, "Thành công");
        }
        private void LoadDatabase()
        {
            try
            {
                if (!string.IsNullOrEmpty(cmbServer.Text))
                {
                    cmbDataBase.Items.Clear();
                    dynamic lst = GetDatabases(cmbServer.Text);
                    foreach (string p in lst)
                    {
                        cmbDataBase.Items.Add(p);
                    }

                    if (cmbDataBase.Items.Count > 0)
                    {
                        string tenDB = Thanh.Utilities.XL_DULIEU.LayGiaTriThanhPhan("Initial Catalog");
                        if (cmbDataBase.Items.Contains(tenDB))
                        {
                            cmbDataBase.Text = tenDB;
                        }
                        else
                        {
                            cmbDataBase.Text = cmbDataBase.Items[0].ToString();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

        private List<string> GetDatabases(string instance)
        {
            try
            {
                List<string> dbList = new List<string>();
                Server sr = new Server(instance);
                foreach (Database db in sr.Databases)
                {
                    dbList.Add(db.Name);
                }
                return dbList;
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }
    }
}
